{include file="common/head"/}
<div class="layui-page-header headers">
    <div class="pagewrap">
        <span class="layui-breadcrumb">
          <a href="{:url('index/index')}">首页</a>
          <a href="javascript:;">{$authruleinfo.pid}</a>
          <a><cite>{$authruleinfo.id}</cite></a>
        </span>
        <a href="javascript:location.replace(location.href);" style="float:right" class="layui-btn layui-btn-blue refreshs"><i class="fa fa-history falocation" aria-hidden="true"></i>
        </a>
    </div>
</div>

<div class="layui-row">
    <div class="layui-card">
        <div class="layui-card-body">
            <blockquote class="layui-elem-quote layui-elem-quote-red" id="boxmain">
                <span id="hand" style="cursor: pointer;">
                    <i class="fa fa-hand-pointer-o" aria-hidden="true" style="font-size: 18px;color: #FF6347"></i>
                    <span style="color:#4476A7">操作提示</span>
                </span>
                <br>
                <span style="font-size: 12px;" id="cons">
                    <span style="color: #FF6347">♠</span>
                    如果用户左侧栏要显示第三层，第二层控制器/方法内容: Show
                </span>
            </blockquote>
            <blockquote class="layui-elem-quote layui-elem-quote-blue">
                <button  tipsmsg="添加" class="layui-btn layui-btns layui-btn-blue onmousemovebtns" onclick="x_admin_show('添加权限','{:url('accessadd')}','610px','610px','95%','95%')"><i class="layui-icon"></i>添加</button>
                <button tipsmsg="展开或折叠全部" class="layui-btn layui-btns layui-btn-green onmousemovebtns" onclick="openAll();"><i class="layui-icon">&#xe857;</i>展开或折叠全部</button>
            </blockquote>
            <table class="layui-hide" id="tableId" lay-filter="tableId"></table>
        </div>
    </div>
</div>
<script type="text/javascript" src="__STATIC__/common/js/jquery.2.1.1.min.js"></script>
{include file="common/foot"/}
<script type="text/javascript" src="__STATIC__/common/js/common.js"></script>
<script type="text/html" id="auth">
    {{# if(d.authopen == 0){ }}
       <input type="checkbox" name="authopen" lay-skin="switch" lay-text="是|否" value="{{d.id}}" lay-filter="authopen" checked>
    {{# }else{  }}
       <input type="checkbox" name="authopen" lay-skin="switch" lay-text="是|否" value="{{d.id}}" lay-filter="authopen">
    {{# } }}
</script>
<script type="text/html" id="levels">
    {{# for (var i=0;i<d.levels;i++){ }}
    <i class="fa fa-star" aria-hidden="true" style="color:#4476A7"></i>
    {{# } }}
</script>
<script type="text/html" id="status">
    <input type="checkbox" name="menustatus" value="{{d.id}}" lay-skin="switch" lay-text="显示|隐藏" lay-filter="menustatus" {{ d.menustatus == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="order">
    <input name="{{d.id}}" data-id="{{d.id}}" class="list_order layui-input" value=" {{d.sort}}" size="10" style="width: 45px;height: 25px;" />
</script>
<script type="text/html" id="icon">
    <i class="fa fa-{{d.icon}}" aria-hidden="true">
</script>
<script type="text/html" id="action">
    <a tipsmsg="添加子菜单" class="layuimsg" onclick="x_admin_show('添加子菜单','{:url('accessadd')}?pid={{d.id}}','610px','610px','95%','95%')">
        <button class="layui-btn layui-btns layui-btn-blue layui-btn-xs">
            <i class="layui-icon"></i>
        </button>
    </a>
    <a tipsmsg="编辑权限" class="layuimsg" onclick="x_admin_show('编辑权限','{:url('accessedit')}?id={{d.id}}','610px','610px','95%','95%')">
    <button class="layui-btn layui-btns layui-btn-green layui-btn-xs">
      <i class="layui-icon">&#xe642;</i>
    </button>
    </a>
    <a tipsmsg="删除" class="layuimsg" lay-event="del">
    <button class="layui-btn layui-btns layui-btn-red layui-btn-xs">
      <i class="layui-icon">&#xe640;</i>
    </button>
    </a>
</script>
<script>
var editObj=null,ptable=null,treeGrid=null,tableId='tableId',layer=null;
    layui.config({
        base: '__STATIC__/common/layui/extend/'
    }).extend({
        treeGrid:'treeGrid'
    }).use(['jquery','treeGrid','layer','form','element'], function(){
        var $=layui.jquery;
        treeGrid = layui.treeGrid;
        layer=layui.layer;
        element = layui.element;
        form = layui.form;
        ptable=treeGrid.render({
            id:tableId
            ,elem: '#'+tableId
            ,idField:'id'
            ,url:'{:url("accesslist")}'
            ,text:{none: '<i class="fa fa-snowflake-o" aria-hidden="true"></i>&nbsp;暂无相关数据'}
            ,cellMinWidth: 100
            ,treeId:'id'//树形id字段名称
            ,treeUpId:'pid'//树形父id字段名称
            ,treeShowName:'title'//以树形式显示的字段
            ,height:'full-140'
            ,isFilter:false
            ,iconOpen:true//是否显示图标【默认显示】
            ,isOpenDefault:false//节点默认是展开还是折叠【默认展开】
            ,cols: [[
                {field: 'id', title: '编号', width: 100,sort:true,align:'center'},
                {field: 'icon', align: 'center',title: '图标', width: 80,templet: '#icon'},
                {field: 'title', title: '权限名称', width: 200},
                {field: 'href', title: '控制器/方法', width: 200},
                {field: 'levels', title: '层级', width: 100,toolbar: '#levels'},
                {field: 'authopen',align: 'center', title: '是否验证权限', width: 120,toolbar: '#auth'},
                {field: 'menustatus',align: 'center',title: '菜单状态', width: 120,toolbar: '#status'},
                {field: 'sort',align: 'center', title: '排序', width: 100, templet: '#order',sort:true},
                {title:'操作',width: 140,align: 'center', toolbar: '#action',fixed: 'right'}
            ]]
            ,page:false
            ,done:function(msg){
                onmousemoves();
               /* console.log(msg.data);*/
            }
        });

        $('#hand').click(function(){
            if($('#cons').css('display')=='none'){
                $('#cons').show(500);
            }else{
                $('#cons').hide(500);
            }

        });

        //修改验证权限状态
        form.on('switch(authopen)', function(obj){
            var urls = "{:url('authopen')}";
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var authopen = obj.elem.checked===true?0:1;
            $.post(urls,{'id':id,'authopen':authopen},function(msg) {
                layer.close(loading);
                if(msg.code == 1){
                    layer.msg(msg.msg);
                    window.location.reload();
                }else if(msg.code == 0) {
                    layer.msg(msg.msg);
                    window.location.reload();
                }else{
                    layer.msg('你无操作权限！');
                    window.location.reload();
                }
            },'json');
        });

        //修改菜单状态
        form.on('switch(menustatus)', function(obj){
            var urls = "{:url('menustatus')}";
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var menustatus = obj.elem.checked===true?1:0;
            $.post(urls,{'id':id,'menustatus':menustatus},function(msg) {
                layer.close(loading);
                if(msg.code == 1){
                    layer.msg(msg.msg);
                    window.location.reload();
                }else if(msg.code == 0) {
                    layer.msg(msg.msg);
                    window.location.reload();
                }else{
                    layer.msg('你无操作权限！');
                    window.location.reload();
                }
            },'json');
        });
       
        //单条信息删除
        treeGrid.on('tool('+tableId+')', function(obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('您确定要删除该分组吗？', {icon: 3,skin: 'layui-layer-lan',anim: 4,title:'删除'}, function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('accessdel')}",{id:data.id},function(res){
                        layer.close(loading);
                        if(res.code===1){
                            layer.msg(res.msg);
                            obj.del();
                        }else if(res.code === 0) {
                            layer.msg(res.msg);
                            window.location.reload();
                        }else{
                            layer.msg('你无操作权限！');
                            window.location.reload();
                        }
                    },'json');
                    layer.close(index);
                });
            }
        });

        $('body').on('blur','.list_order',function() {
           var id = $(this).attr('data-id');
           var sort = $(this).val();
           $.post('{:url("rulesort")}',{id:id,sort:sort},function(res){
                if(res.code==1){
                    layer.msg(res.msg);
                    window.location.reload();
                }else if(res.code === 0) {
                    layer.msg(res.msg);
                    window.location.reload();
                }else{
                    layer.msg('你无操作权限！');
                    window.location.reload();
                }
           })
        })
    });

function openAll() {
    var treedata=treeGrid.getDataTreeList(tableId);
    treeGrid.treeOpenAll(tableId,!treedata[0][treeGrid.config.cols.isOpen]);
}
</script>